package com.cspm.ahjy.bc.office.word.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.StringUtils;

import com.cspm.ahjy.bc.office.word.domain.MSAction;
import com.cspm.ahjy.bc.office.word.domain.MSActionType;
import com.cspm.ahjy.bc.office.word.domain.MSTableAction;

/**
 * 原子操作工具类
 * @author ServerZhang
 * @date 2017年10月9日
 */
public class ActionUtil {
	
	public static MSAction getMsAction4Json(String jsonStr){
		MSAction action = new MSAction();
		
		Boolean canDo = true;
		// 检查字符串是否合法
		canDo = jsonStr.startsWith("{") && jsonStr.endsWith("}");
		if (!canDo) {
			return null;
		}

		JSONObject jsonObject = new JSONObject(jsonStr);
		
		Object actionPreObj = jsonObject.get("actionPre");
		if (actionPreObj != null) {
			action.setActionPre(actionPreObj.toString());
		}
		Object insertObj = jsonObject.get("insert");
		if (insertObj != null) {
			action.setInsert(Boolean.parseBoolean(insertObj.toString()));
		}
		Object pObject = jsonObject.get("paragraphNos");
		if (pObject != null && pObject.toString().startsWith("[") && pObject.toString().endsWith("]")) {
			JSONArray paragraphArray = jsonObject.getJSONArray("paragraphNos");
			Iterator<Object> pIterator = paragraphArray.iterator();
			Integer[] paragraphs = new Integer[paragraphArray.length()];
			int i=0;
			while(pIterator.hasNext()){
				Object next = pIterator.next();
				if (next == null) {
					continue;
				}
				paragraphs[i] = Integer.parseInt(next.toString());
				i++;
			}
			action.setParagraphNos(paragraphs);
		}
		Object textObj = jsonObject.get("text");
		if (textObj != null) {
			action.setText(textObj.toString());
		}
		Object tObject = jsonObject.get("textNos");
		if (tObject != null && tObject.toString().startsWith("[") && tObject.toString().endsWith("]")) {
			JSONArray textNosArray = jsonObject.getJSONArray("textNos");
			Iterator<Object> tIterator = textNosArray.iterator();
			Integer[] textNos = new Integer[textNosArray.length()];
			int j=0;
			while(tIterator.hasNext()){
				Object next = tIterator.next();
				if (next == null) {
					continue;
				}
				textNos[j] = Integer.parseInt(next.toString());
				j++;
			}
			action.setTextNos(textNos);
		}
		Object msActionTypeObj = jsonObject.get("msActionType");
		if (!StringUtils.isEmpty(msActionTypeObj)) {
			action.setMsActionType(ActionTypeUtil.getMsActionType4Json(msActionTypeObj.toString()));
		}
		
		Object msTableActionObj = jsonObject.get("msTableAction");
		if (!StringUtils.isEmpty(msTableActionObj)) {
			action.setMsTableAction(TableActionUtil.getMSTableAction4Json(msTableActionObj.toString()));
		}
		
		return action;
	}

	public static void printActions(List<MSAction> actions){
		for(MSAction action : actions){
			printAction(action);
		}
	}
	public static void printAction(MSAction action){
		MSTableAction msTableAction = action.getMsTableAction();
		if (msTableAction != null) {
			TableActionUtil.printAction(msTableAction);
			return;
		}
		String actionPre = action.getActionPre();
		MSActionType msActionType = action.getMsActionType();
		if (actionPre == null || msActionType == null) {
			return;
		}
		String typeContent = msActionType.getTypeContent();
		
		System.out.println(actionPre+"："+typeContent);
	}
	
	/**
	 * 装饰Word的一个操作，对其生成操作前缀
	 * @param msAction
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月19日
	 */
	public static MSAction dekorateAction(MSAction msAction){
		MSAction action = msAction;
		
		Integer[] paragraphNos = msAction.getParagraphNos();
		String text = msAction.getText();
		Integer[] textNos = msAction.getTextNos();
		
		MSTableAction tableAction = msAction.getMsTableAction();
		if (tableAction != null) {
			MSTableAction dekorateAction = TableActionUtil.dekorateAction(tableAction);
			action.setMsTableAction(dekorateAction);
			return action;
		}
		
		MSActionType msActionType = msAction.getMsActionType();
		if (msActionType == null) {
			return action;
		}
		String type = msActionType.getType();
		String typeContent = msActionType.getTypeContent();
		Object typeValue = msActionType.getTypeValue();
		
		Boolean insert = msAction.getInsert();
		
		StringBuffer inforBuffer = new StringBuffer();
		
		if (paragraphNos == null) {
			if ("body_paragraph".equals(type)) {
				//inforBuffer.append(typeContent);
			}else {
				inforBuffer.append("设置页面格式为");
			}
		}else if (paragraphNos.length == 0) {
			inforBuffer.append("将正文格式设置为");
		}else if (paragraphNos.length == 1 && paragraphNos[0].equals(0)) {
			inforBuffer.append("将标题");
			if (text == null) {//非文本操作
				inforBuffer.append("设置格式");
			}
		}else {
			inforBuffer.append("将第");
			for(int i=0,length=paragraphNos.length; i<length; i++){
				inforBuffer.append(paragraphNos[i]);
				if (i != length - 1) {
					inforBuffer.append("、");
				}
			}
			inforBuffer.append("段");
			if (text == null) {//非文本操作
				inforBuffer.append("设置格式");
			}
		}
		
		if (text == null) {//非文本操作
		}else if (text.equals("")) {
			inforBuffer.append("中的所有文字设置格式");
		}else {
			inforBuffer.append("中的");
			if (textNos == null || textNos.length == 0) {
			}else{
				inforBuffer.append("第");
				for(int i=0,length=textNos.length; i<length; i++){
					inforBuffer.append(textNos[i]);
					if (i != length - 1) {
						inforBuffer.append("、");
					}
				}
				inforBuffer.append("个");
				
			}
			if ("/picture/".equals(text)) {
				inforBuffer = new StringBuffer();
				if (insert) {
					inforBuffer.append("在第");
					for(int i=0,length=paragraphNos.length; i<length; i++){
						inforBuffer.append(paragraphNos[i]);
						if (i != length - 1) {
							inforBuffer.append("、");
						}
					}
					inforBuffer.append("段后插入图片，格式");
				}else {
					inforBuffer.append("修改第");
					for(int i=0,length=paragraphNos.length; i<length; i++){
						inforBuffer.append(paragraphNos[i]);
						if (i != length - 1) {
							inforBuffer.append("、");
						}
					}
					inforBuffer.append("段的图片").append("格式");
				}
			}else {
				inforBuffer.append("文字 “").append(text).append("”设置格式");
			}
		}
		
		action.setActionPre(inforBuffer.toString());
		
		return action;
	}
	
	/**
	 * 打印出对Word的所有操作项
	 * @param actions
	 * @author ServerZhang
	 * @date 2017年10月9日
	 */
	public static List<MSAction> dekorateActions(List<MSAction> actions){
		List<MSAction> list = new ArrayList<MSAction>();
		
		/*if (actions.size() == 0) {
			//System.out.println("震惊，两篇文档居然...");
			System.out.println("Perfect!!!");
			return;
		}*/
		Iterator<MSAction> iterator = actions.iterator();
		while(iterator.hasNext()){
			MSAction next = iterator.next();
			MSAction dekorateAction = dekorateAction(next);
			list.add(dekorateAction);
		}
		
		return list;
	}
	
	/**
	 * 原子操作合并
	 * @param actions
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月11日
	 */
	public static List<MSAction> merge(List<MSAction> actions){
		List<MSAction> mergedList = new ArrayList<MSAction>();
		
		//TODO 
		
		return mergedList;
	}
	/**
	 * 原子操作合并－合并不同段落的相同操作
	 * @param actions
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月11日
	 */
	public static List<MSAction> merge4Paragraph(List<MSAction> actions){
		List<MSAction> mergedList = new ArrayList<MSAction>();
		
		Iterator<MSAction> iterator = actions.iterator();
		while(iterator.hasNext()){
			MSAction next = iterator.next();
			
			Integer[] paragraphNos = next.getParagraphNos();
			if (null == paragraphNos || paragraphNos.length == 0) {//没有段落的，不需要合并
				mergedList.add(next);
				continue;
			}
			
			Boolean insert = next.getInsert();
			MSActionType actionType = next.getMsActionType();
			Iterator<MSAction> iterator2 = mergedList.iterator();
			Boolean merged = false;
			while(iterator2.hasNext()){
				MSAction next2 = iterator2.next();
				Integer[] paragraphNos2 = next2.getParagraphNos();
				if (null == paragraphNos2 || paragraphNos2.length == 0) {//没有段落的，不需要合并
					continue;
				}
				
				Boolean insert2 = next2.getInsert();
				MSActionType actionType2 = next2.getMsActionType();
				
				if (actionType.equals(actionType2) && insert.equals(insert2)) {//不同段落的相同操作，仅存在一个
					int length = paragraphNos.length;
					int length2 = paragraphNos2.length;
					Integer[] pNos = new Integer[length+length2];
					for(int i=0; i<length2; i++){
						pNos[i] = paragraphNos2[i];
					}
					for(int i=0; i<length; i++){
						pNos[length2+i] = paragraphNos[i];
					}
					next2.setParagraphNos(pNos);
					merged = true;
					break;//因为仅存在一个，所以此处可以跳出循环
				}
			}
			//没有相同操作，直接放入
			if (!merged) {
				mergedList.add(next);
			}
			
		}
		
		return mergedList;
	}
	
}
